package controller

import (
	"github.com/gofiber/fiber/v2"
	"log"
	"newRetailByGolang/model"
	"newRetailByGolang/service"
)

type RetailController struct {
	retailService *service.RetailService
	orderService  *service.OrderService
}

func NewRetailController() *RetailController {
	return &RetailController{
		retailService: service.NewRetailService(),
		orderService:  service.NewOrderService(),
	}
}

func (rc *RetailController) StatisticalActivities(c *fiber.Ctx) error {

	//rc.retailService = new(service.RetailService)
	//rc.retailService.ParticipationRecord = new(model.ParticipationRecord)
	//rc.retailService.RegistrationActivitie = new(model.RegistrationActivitie)
	//rc.retailService.User = new(model.User)
	//rc.retailService.UpgradeRecord = new(model.UpgradeRecord)
	//rc.retailService.Store = new(model.Store)
	//rc.retailService.TaskRecord = new(model.TaskRecord)

	err := rc.retailService.HandleTask()
	if err != nil {
		c.JSON(fiber.Map{
			"code":    500,
			"data":    nil,
			"message": err.Error(),
		})
		return nil
	}

	c.JSON(fiber.Map{
		"code":    200,
		"data":    "",
		"message": "计算成功",
	})
	return nil
}

func (rc *RetailController) PlaceOrder(c *fiber.Ctx) error {
	o := new(model.Order)

	if err := c.BodyParser(o); err != nil {
		log.Fatal(err)
	}

	res, err := rc.orderService.InsertOrder(o)
	if err != nil {
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
		})
		return nil
	}

	c.JSON(fiber.Map{
		"code":    200,
		"data":    res["data"],
		"message": "成功",
	})
	return nil
}

func (rc *RetailController) PlaceOrderInPool(c *fiber.Ctx) error {
	o := new(model.Order)

	if err := c.BodyParser(o); err != nil {
		log.Fatal(err)
	}

	res, err := rc.orderService.InsertOrderInPool(o)
	if err != nil {
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
		})
		return nil
	}

	c.JSON(fiber.Map{
		"code":    200,
		"data":    res["data"],
		"message": "成功",
	})
	return nil
}

func (rc *RetailController) PlaceOrderInRedisQueue(c *fiber.Ctx) error {
	o := new(model.Order)

	if err := c.BodyParser(o); err != nil {
		log.Fatal(err)
	}

	res, err := rc.orderService.InsertOrderInRedisQueue(o)
	if err != nil {
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
		})
		return nil
	}

	c.JSON(fiber.Map{
		"code":    200,
		"data":    res["data"],
		"message": "成功",
	})
	return nil
}

func (rc *RetailController) NewGetCatalogueInfo(c *fiber.Ctx) error {
	//account := c.Params("account")
	//log.Println("account：", account)

	path := c.Path()
	log.Println("path：", path)

	c.JSON(fiber.Map{
		"code":    200,
		"data":    rc.orderService.NewGetFiles("F:/" + path),
		"message": "成功",
	})
	return nil
}
